/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef __UART_G9X_H__
#define __UART_G9X_H__

#include <plat/device.h>
#include <stdlib/stdint.h>

#define __I  volatile const
#define __O  volatile
#define __IO volatile

typedef union {
    struct {
        uint32_t RBR8 : 8;
        uint32_t RSVD_RBR24 : 24;
    };
    uint32_t v;
} RBR_Type;

typedef union {
    struct {
        uint32_t DLL : 8;
        uint32_t RSVD_DLL_31to8 : 24;
    };
    uint32_t v;
} DLL_Type;

typedef union {
    struct {
        uint32_t THR8 : 8;
        uint32_t RSVD_THR_31to8 : 24;
    };
    uint32_t v;
} THR_Type;

typedef union {
    struct {
        uint32_t DLH : 8;
        uint32_t RSVD_DLH : 24;
    };
    uint32_t v;
} DLH_Type;

typedef union {
    struct {
        uint32_t ERBFI : 1;
        uint32_t ETBEI : 1;
        uint32_t ELSI : 1;
        uint32_t EDSSI : 1;
        uint32_t ELCOLR : 1;
        uint32_t RSVD_IER_6TO5 : 2;
        uint32_t PTIME : 1;
        uint32_t RSVD_IER_31TO8 : 24;
    };
    uint32_t v;
} IER_Type;

typedef union {
    struct {
        uint32_t FIFOE : 1;
        uint32_t RFIFOR : 1;
        uint32_t XFIFOR : 1;
        uint32_t DMAM : 1;
        uint32_t TET : 2;
        uint32_t RT : 2;
        uint32_t RSVD_FCR_31to8 : 24;
    };
    uint32_t v;
} FCR_Type;

typedef union {
    struct {
        uint32_t IID : 4;
        uint32_t RSVD_IIR_5TO4 : 2;
        uint32_t FIFOSE : 2;
        uint32_t RSVD_IIR_31TO8 : 24;
    };
    uint32_t v;
} IIR_Type;

typedef union {
    struct {
        uint32_t DLS : 2;
        uint32_t STOP : 1;
        uint32_t PEN : 1;
        uint32_t EPS : 1;
        uint32_t SP : 1;
        uint32_t BC : 1;
        uint32_t DLAB : 1;
        uint32_t RSVD_LCR_31TO8 : 24;
    };
    uint32_t v;
} LCR_Type;

typedef union {
    struct {
        uint32_t DTR : 1;
        uint32_t RTS : 1;
        uint32_t OUT1 : 1;
        uint32_t OUT2 : 1;
        uint32_t LOOPBACK : 1;
        uint32_t AFCE : 1;
        uint32_t SIRE : 1;
        uint32_t RSVD_MCR_31TO7 : 25;
    };
    uint32_t v;
} MCR_Type;

typedef union {
    struct {
        uint32_t DR : 1;
        uint32_t OE : 1;
        uint32_t PE : 1;
        uint32_t FE : 1;
        uint32_t BI : 1;
        uint32_t THRE : 1;
        uint32_t TEMT : 1;
        uint32_t RFE : 1;
        uint32_t ADDR_RCVD : 1;
        uint32_t RSVD_LSR_31TO9 : 23;
    };
    uint32_t v;
} LSR_Type;

typedef union {
    struct {
        uint32_t DCTS : 1;
        uint32_t DDSR : 1;
        uint32_t TERI : 1;
        uint32_t DDCD : 1;
        uint32_t CTS : 1;
        uint32_t DSR : 1;
        uint32_t RI : 1;
        uint32_t DCD : 1;
        uint32_t RSVD_MSR_31TO8 : 24;
    };
    uint32_t v;
} MSR_Type;

typedef union {
    struct {
        uint32_t SCR : 8;
        uint32_t RSVD_SCR_31TO8 : 24;
    };
    uint32_t v;
} SCR_Type;

typedef union {
    struct {
        uint32_t SRBRN : 9;
        uint32_t RSVD_SRBRN : 23;
    };
    uint32_t v;
} SRBRX_Type;

typedef union {
    struct {
        uint32_t FAR : 1;
        uint32_t RSVD_FAR_31TO1 : 31;
    };
    uint32_t v;
} FAR_Type;

typedef union {
    struct {
        uint32_t TFR : 8;
        uint32_t RSVD_TFR_31TO8 : 24;
    };
    uint32_t v;
} TFR_Type;

typedef union {
    struct {
        uint32_t RFWD : 8;
        uint32_t RFPE : 1;
        uint32_t RFFE : 1;
        uint32_t RSVD_RFW_31TO10 : 22;
    };
    uint32_t v;
} RFW_Type;

typedef union {
    struct {
        uint32_t RSVD_BUSY : 1;
        uint32_t TFNF : 1;
        uint32_t TFE : 1;
        uint32_t RFNE : 1;
        uint32_t RFF : 1;
        uint32_t RSVD_USR_31TO5 : 27;
    };
    uint32_t v;
} USR_Type;

typedef union {
    struct {
        uint32_t TFL : 6;
        uint32_t RSVD_TFL_31TOADDR_WIDTH : 26;
    };
    uint32_t v;
} TFL_Type;

typedef union {
    struct {
        uint32_t RFL : 6;
        uint32_t RSVD_RFL_31TOADDR_WIDTH : 26;
    };
    uint32_t v;
} RFL_Type;

typedef union {
    struct {
        uint32_t UR : 1;
        uint32_t RFR : 1;
        uint32_t XFR : 1;
        uint32_t RSVD_SRR_31TO3 : 29;
    };
    uint32_t v;
} SRR_Type;

typedef union {
    struct {
        uint32_t SRTS : 1;
        uint32_t RSVD_SRTS_31TO1 : 31;
    };
    uint32_t v;
} SRTS_Type;

typedef union {
    struct {
        uint32_t SBCB : 1;
        uint32_t RSVD_SBCR_31TO1 : 31;
    };
    uint32_t v;
} SBCR_Type;

typedef union {
    struct {
        uint32_t SDMAM : 1;
        uint32_t RSVD_SDMAM_31TO1 : 31;
    };
    uint32_t v;
} SDMAM_Type;

typedef union {
    struct {
        uint32_t SFE : 1;
        uint32_t RSVD_SFE_31TO1 : 31;
    };
    uint32_t v;
} SFE_Type;

typedef union {
    struct {
        uint32_t SRT : 2;
        uint32_t RSVD_SRT_31TO2 : 30;
    };
    uint32_t v;
} SRT_Type;

typedef union {
    struct {
        uint32_t STET : 2;
        uint32_t RSVD_STET_31TO2 : 30;
    };
    uint32_t v;
} STET_Type;

typedef union {
    struct {
        uint32_t HTX : 1;
        uint32_t RSVD_HTX_31TO1 : 31;
    };
    uint32_t v;
} HTX_Type;

typedef union {
    struct {
        uint32_t DMASA : 1;
        uint32_t RSVD_DMASA_31TO1 : 31;
    };
    uint32_t v;
} DMASA_Type;

typedef union {
    struct {
        uint32_t RS485_EN : 1;
        uint32_t RE_POL : 1;
        uint32_t DE_POL : 1;
        uint32_t XFER_MODE : 2;
        uint32_t RSVD_TCR_31TO5 : 27;
    };
    uint32_t v;
} TCR_Type;

typedef union {
    struct {
        uint32_t DE_ENABLE : 1;
        uint32_t RSVD_DE_EN_31TO1 : 31;
    };
    uint32_t v;
} DE_EN_Type;

typedef union {
    struct {
        uint32_t RE_ENABLE : 1;
        uint32_t RSVD_RE_EN_31TO1 : 31;
    };
    uint32_t v;
} RE_EN_Type;

typedef union {
    struct {
        uint32_t DE_ASSERTION_TIME : 8;
        uint32_t RSVD_DE_AT_15TO8 : 8;
        uint32_t DE_DEASSERTION_TIME : 8;
        uint32_t RSVD_DE_DEAT_31TO24 : 8;
    };
    uint32_t v;
} DET_Type;

typedef union {
    struct {
        uint32_t DE_TO_RE : 16;
        uint32_t RE_TO_DE : 16;
    };
    uint32_t v;
} TAT_Type;

typedef union {
    struct {
        uint32_t DLF : 4;
        uint32_t RSVD_DLF : 28;
    };
    uint32_t v;
} DLF_Type;

typedef union {
    struct {
        uint32_t RAR : 8;
        uint32_t RSVD_RAR_31TO8 : 24;
    };
    uint32_t v;
} RAR_Type;

typedef union {
    struct {
        uint32_t TAR : 8;
        uint32_t RSVD_TAR_31TO8 : 24;
    };
    uint32_t v;
} TAR_Type;

typedef union {
    struct {
        uint32_t DLS_E : 1;
        uint32_t ADDR_MATCH : 1;
        uint32_t SEND_ADDR : 1;
        uint32_t TRANSMIT_MODE : 1;
        uint32_t RSVD_LCR_EXT : 28;
    };
    uint32_t v;
} LCR_EXT_Type;

typedef union {
    struct {
        uint32_t APB_DATA_WIDTH : 2;
        uint32_t RSVD_CPR_3TO2 : 2;
        uint32_t AFCE_MODE : 1;
        uint32_t THRE_MODE : 1;
        uint32_t SIR_MODE : 1;
        uint32_t SIR_LP_MODE : 1;
        uint32_t ADDITIONAL_FEAT : 1;
        uint32_t FIFO_ACCESS : 1;
        uint32_t FIFO_STAT : 1;
        uint32_t SHADOW : 1;
        uint32_t UART_ADD_ENCODED_PARAMS : 1;
        uint32_t DMA_EXTRA : 1;
        uint32_t RSVD_CPR_15TO14 : 2;
        uint32_t FIFO_MODE : 8;
        uint32_t RSVD_CPR_31TO24 : 8;
    };
    uint32_t v;
} CPR_Type;

typedef union {
    struct {
        uint32_t UART_COMPONENT_VERSION;
    };
    uint32_t v;
} UCV_Type;

typedef union {
    struct {
        uint32_t PERIPHERAL_ID;
    };
    uint32_t v;
} CTR_Type;

typedef struct {
    union {
        __IO RBR_Type RBR;
        __IO DLL_Type DLL;
        __IO THR_Type THR;
    };
    union {
        __IO DLH_Type DLH;
        __IO IER_Type IER;
    };
    union {
        __IO FCR_Type FCR;
        __IO IIR_Type IIR;
    };
    __IO LCR_Type     LCR;
    __IO MCR_Type     MCR;
    __IO LSR_Type     LSR;
    __IO MSR_Type     MSR;
    __IO SCR_Type     SCR;
    __IO uint32_t     RESERVED0;
    __IO uint32_t     RESERVED1;
    __IO uint32_t     RESERVED2;
    __IO uint32_t     RESERVED3;
    __IO SRBRX_Type   SRBR0;
    __IO SRBRX_Type   SRBR1;
    __IO SRBRX_Type   SRBR2;
    __IO SRBRX_Type   SRBR3;
    __IO SRBRX_Type   SRBR4;
    __IO SRBRX_Type   SRBR5;
    __IO SRBRX_Type   SRBR6;
    __IO SRBRX_Type   SRBR7;
    __IO SRBRX_Type   SRBR8;
    __IO SRBRX_Type   SRBR9;
    __IO SRBRX_Type   SRBR10;
    __IO SRBRX_Type   SRBR11;
    __IO SRBRX_Type   SRBR12;
    __IO SRBRX_Type   SRBR13;
    __IO SRBRX_Type   SRBR14;
    __IO SRBRX_Type   SRBR15;
    __IO FAR_Type     FAR_;
    __IO TFR_Type     TFR;
    __IO RFW_Type     RFW;
    __IO USR_Type     USR;
    __IO TFL_Type     TFL;
    __IO RFL_Type     RFL;
    __IO SRR_Type     SRR;
    __IO SRTS_Type    SRTS;
    __IO SBCR_Type    SBCR;
    __IO SDMAM_Type   SDMAM;
    __IO SFE_Type     SFE;
    __IO SRT_Type     SRT;
    __IO STET_Type    STET;
    __IO HTX_Type     HTX;
    __IO DMASA_Type   DMASA;
    __IO TCR_Type     TCR;
    __IO DE_EN_Type   DE_EN;
    __IO RE_EN_Type   RE_EN;
    __IO DET_Type     DET;
    __IO TAT_Type     TAT;
    __IO DLF_Type     DLF;
    __IO RAR_Type     RAR;
    __IO TAR_Type     TAR;
    __IO LCR_EXT_Type LCR_EXT;
    __IO uint32_t     RESERVED4;
    __IO uint32_t     RESERVED5;
    __IO uint32_t     RESERVED6;
    __IO uint32_t     RESERVED7;
    __IO uint32_t     RESERVED8;
    __IO uint32_t     RESERVED9;
    __IO uint32_t     RESERVED10;
    __IO uint32_t     RESERVED11;
    __IO uint32_t     RESERVED12;
    __IO CPR_Type     CPR;
    __IO UCV_Type     UCV;
    __IO CTR_Type     CTR;
} DW_APB_UART_uart_TypeDef;

void serial_init(unsigned int clk);
char serial_getchar(void);
void serial_putchar(const char c);
#endif
